// 交易平台配置
export interface TradingConfig {
  dataSource: 'binance' | 'coingecko' | 'mock';
  enableRealtime: boolean;
  enableWebsocket: boolean;
  refreshInterval: number; // 毫秒
  defaultSymbol: string;
  supportedSymbols: string[];
}

// 默认配置
export const defaultConfig: TradingConfig = {
  dataSource: 'mock', // 改为 'binance' 可以使用真实数据
  enableRealtime: true,
  enableWebsocket: true,
  refreshInterval: 5000,
  defaultSymbol: 'BTCUSDT',
  supportedSymbols: [
    'BTCUSDT',
    'ETHUSDT', 
    'SOLUSDT',
    'ADAUSDT',
    'DOTUSDT',
    'LINKUSDT',
    'MATICUSDT',
    'AVAXUSDT',
    'UNIUSDT',
    'LTCUSDT'
  ],
};

// 环境变量配置
export const getConfigFromEnv = (): Partial<TradingConfig> => {
  return {
    dataSource: (process.env.NEXT_PUBLIC_DATA_SOURCE as TradingConfig['dataSource']) || defaultConfig.dataSource,
    enableRealtime: process.env.NEXT_PUBLIC_ENABLE_REALTIME === 'true',
    enableWebsocket: process.env.NEXT_PUBLIC_ENABLE_WEBSOCKET === 'true',
  };
};

// 合并配置
export const getConfig = (): TradingConfig => {
  const envConfig = getConfigFromEnv();
  return { ...defaultConfig, ...envConfig };
};

// API端点配置
export const API_ENDPOINTS = {
  binance: {
    rest: 'https://api.binance.com/api/v3',
    websocket: 'wss://stream.binance.com:9443/ws',
  },
  coinbase: {
    rest: 'https://api.exchange.coinbase.com',
    websocket: 'wss://ws-feed.exchange.coinbase.com',
  },
  coingecko: {
    rest: 'https://api.coingecko.com/api/v3',
  },
};

// 符号映射（不同交易所可能有不同的符号格式）
export const SYMBOL_MAPPING = {
  'BTC/USDT': {
    binance: 'BTCUSDT',
    coinbase: 'BTC-USD',
    coingecko: 'bitcoin',
  },
  'ETH/USDT': {
    binance: 'ETHUSDT',
    coinbase: 'ETH-USD', 
    coingecko: 'ethereum',
  },
  'SOL/USDT': {
    binance: 'SOLUSDT',
    coinbase: 'SOL-USD',
    coingecko: 'solana',
  },
  // 可以继续添加更多映射
};

// 获取特定交易所的符号格式
export const getSymbolForExchange = (
  displaySymbol: string, 
  exchange: keyof typeof SYMBOL_MAPPING[keyof typeof SYMBOL_MAPPING]
): string => {
  const mapping = SYMBOL_MAPPING[displaySymbol as keyof typeof SYMBOL_MAPPING];
  return mapping ? mapping[exchange] : displaySymbol.replace('/', '');
};

// 交易对信息
export const TRADING_PAIRS = [
  {
    symbol: 'BTC/USDT',
    name: 'Bitcoin',
    baseAsset: 'BTC',
    quoteAsset: 'USDT',
    minTradeAmount: 0.00001,
    priceDecimals: 2,
    quantityDecimals: 6,
  },
  {
    symbol: 'ETH/USDT', 
    name: 'Ethereum',
    baseAsset: 'ETH',
    quoteAsset: 'USDT',
    minTradeAmount: 0.0001,
    priceDecimals: 2,
    quantityDecimals: 4,
  },
  {
    symbol: 'SOL/USDT',
    name: 'Solana', 
    baseAsset: 'SOL',
    quoteAsset: 'USDT',
    minTradeAmount: 0.01,
    priceDecimals: 2,
    quantityDecimals: 2,
  },
  // 可以继续添加更多交易对
];

// 获取交易对信息
export const getTradingPairInfo = (symbol: string) => {
  return TRADING_PAIRS.find(pair => pair.symbol === symbol);
};
